package com.abe.abetest.alarm;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.IBinder;

import com.abe.abetest.R;
import com.abe.abetest.utils.CommonUtils;

public class AlarmService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        CommonUtils.log("AlarmService--onCreate");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        CommonUtils.log("AlarmService-onStartCommand");
        Notification.Builder builder = new Notification.Builder(this.getApplicationContext());
        Intent nfIntent = new Intent(this, AlarmShowActivity.class);
        if (intent.getExtras() != null) {
            CommonUtils.log("AlarmService-intent.getExtras() != null");
            nfIntent.putExtras(intent.getExtras());
            boolean needRepeating = intent.getBooleanExtra("needRepeating", false);
            CommonUtils.wf("AlarmService-needRepeating:%b", needRepeating);
            if (needRepeating) {
                int id = intent.getIntExtra("id", 0);
                int count = intent.getIntExtra("count", 1);
                int time = intent.getIntExtra("time", 0);
                int intervalTime = intent.getIntExtra("intervalTime", 0);
                AlarmHelper.setAlarm(this, id, count, time, intervalTime,
                        intent.getExtras().getString("path"), intent.getExtras().getString("data"));
            }
        }
        builder.setContentIntent(PendingIntent.getActivity(this, (int) System.currentTimeMillis(), nfIntent, 0))
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
                .setContentTitle("闹钟") // 设置下拉列表里的标题
                .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
                .setContentText("闹钟介绍") // 设置上下文内容
                .setAutoCancel(true)//点击后消失
                .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间
        Notification notification = builder.build(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        // 参数一：唯一的通知标识；参数二：通知消息。
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify((int) System.currentTimeMillis(), notification);
        CommonUtils.log("AlarmService-notification");
        return super.onStartCommand(intent, flags, startId);
    }
}